﻿
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports requestProcessors_share

Public Class diagnostic
    Inherits controlProcessor

    Public _pathidentity As String = "/diagnostic"

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Shared Function createXHtmlPage() As xhtmlpage
        Return createXHtmlPage(httpServer2.currentContext())
    End Function

    Private Shared Sub addrequestInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")

        addkeyvaluepair(context, root, "ip", context.requestIp())
        addkeyvaluepair(context, root, "url", context.url().ToString())
        addkeyvaluepair(context, root, "protocol", context.url().protocol)
        addkeyvaluepair(context, root, "host", context.url().host())
        addkeyvaluepair(context, root, "port", context.url().port)
        addkeyvaluepair(context, root, "user", context.url().user)
        addkeyvaluepair(context, root, "password", context.url().password)
        addkeyvaluepair(context, root, "path", context.url().path())
        addkeyvaluepair(context, root, "query", context.url().queryString())
        addkeyvaluepair(context, root, "position", context.url().position)
        addkeyvaluepair(context, root, "unknown", context.url().unknown)
        addkeyvaluepair(context, root, "referer", context.referer())
        addkeyvaluepair(context, root, "language", context.language())
        addkeyvaluepair(context, root, "traceID", context.traceID())
        addkeyvaluepair(context, root, "userAgent", context.userAgent())
        addkeyvaluepair(context, root, "isBrowserUserAgent", Convert.ToString(context.isBrowserUserAgent()))
        addkeyvaluepair(context, root, "isIEUserAgent", Convert.ToString(context.isIEUserAgent()))
        addkeyvaluepair(context, root, "isinternalRequest", Convert.ToString(context.isinternalRequest()))
    End Sub

    Private Shared Sub addqueryInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")

        Dim it As map(Of String, String).iterator = Nothing
        it = context.url().query.begin()
        While it <> context.url().query.end()
            addkeyvaluepair(context, root, (+it).first(), (+it).second)
            it += 1
        End While
    End Sub

    Private Shared Sub addcookieInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        If Not context.cookies() Is Nothing AndAlso context.cookies().size() > 0 Then
            Dim ul As ul = Nothing
            ul = context.createControl(New ul())
            root.addchild(ul)

            Dim it As map(Of String, map(Of String, String)).iterator = Nothing
            it = context.cookies().begin()
            While it <> context.cookies().end()
                Dim li As li = Nothing
                li = context.createControl(New li())
                ul.addchild(li)

                Dim xmltext As xmltext = Nothing
                xmltext = context.createControl(New xmltext())
                xmltext.text = "cookies for domain " + (+it).first()
                li.addchild(xmltext)

                Dim it2 As map(Of String, String).iterator = Nothing
                it2 = (+it).second.begin()
                While it2 <> (+it).second.end()
                    addkeyvaluepair(context, li, (+it2).first(), (+it2).second)
                    it2 += 1
                End While

                it += 1
            End While
        End If
    End Sub

    Private Shared Sub addformdataInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")

        If Not context.formdatas() Is Nothing Then
            Dim it As map(Of String, formdata).iterator = Nothing
            it = context.formdatas().begin()
            While it <> context.formdatas().end()
                addkeyvaluepair(context, root, (+it).first() + "_name", (+it).second.name())
                addkeyvaluepair(context, root, (+it).first() + "_content", (+it).second.content())
                addkeyvaluepair(context, root, (+it).first() + "_filename", (+it).second.filename())
                addkeyvaluepair(context, root, (+it).first() + "_tempfilename", (+it).second.tempfilename())

                it += 1
            End While
        End If
    End Sub

    Private Shared Sub addfilters(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")

        Dim it As map(Of String, String).iterator = Nothing
        it = context.filters().begin()
        While it <> context.filters().end()
            addkeyvaluepair(context, root, (+it).first(), (+it).second)
            it += 1
        End While

        If context.hasstrings() Then
            addkeyvaluepair(context, root, "strings selected", context.strings().language())
        End If
    End Sub

    Private Shared Sub addoriginalRequestInfo(ByVal context As httpContext, ByVal root As xmlcontrol)
        assert(Not context Is Nothing, "context is nothing.")
        assert(Not root Is Nothing, "root is nothing.")

        addkeyvaluepair(context, root, "url", requestFullUrl(context.request()))
        addkeyvaluepair(context, root, "ip", requestIp(context.request()))
        addkeyvaluepair(context, root, "traceId", traceID(context.request()))
    End Sub

    Public Shared Function createXHtmlPage(ByVal context As httpContext) As xhtmlpage
        assert(Not context Is Nothing, "context is nothing.")
        Dim rtn As xhtmlpage = Nothing
        rtn = context.createControl(New xhtmlpage())
        rtn.title().text = "diagnostic results"

        Dim ul As ul = Nothing
        ul = context.createControl(New ul())
        rtn.body().addchild(ul)

        Dim li As li = Nothing
        li = context.createControl(New li())
        ul.addchild(li)
        Dim xmltext As xmltext = Nothing
        xmltext = context.createControl(New xmltext())
        xmltext.text = "request info"
        li.addchild(xmltext)
        addrequestInfo(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "query detail"
        li.addchild(xmltext)
        addqueryInfo(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "cookies detail"
        li.addchild(xmltext)
        addcookieInfo(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "formdatas detail"
        li.addchild(xmltext)
        addformdataInfo(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "context filters"
        li.addchild(xmltext)
        addfilters(context, li)

        li = context.createControl(New li())
        ul.addchild(li)
        xmltext = context.createControl(New xmltext())
        xmltext.text = "original request info"
        li.addchild(xmltext)
        addoriginalRequestInfo(context, li)

        Return rtn
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        control = createXHtmlPage(context)
        Return True
    End Function
End Class
